package org.iteam.xlz.core.service.membershipcard;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.recharge.RechargeRecord;

public class RecordService extends BaseService<RechargeRecord>{
	private static RecordService instance;
	
	private RecordService(){}
	
	public static RecordService getInstance(){
		if(null==instance){
			instance=new RecordService();
		}
		return instance;
	}
	
	public long counts(String mobile){
		try {
			StringBuilder sql=new StringBuilder("select count(1) from rechargerecord l left join userinfo r on l.uid=r.uid where 1=1");
			List<String> params=new ArrayList<String>();
			if(StringUtils.isNotBlank(mobile)){
				sql.append(" and r. mobile=?");
				params.add(mobile);
			}
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<RechargeRecord> query(String mobile,long page,long rows){
		try {
			List<Object> params=new ArrayList<Object>();
			StringBuilder sql=new StringBuilder("select l.rrid,r.mobile as uid,s.rechargename as rid,l.createtime"+
		" from rechargerecord l left join userinfo r on l.uid=r.uid left join recharges s on  l.rid=s.rid where 1=1");
		if(StringUtils.isNotBlank(mobile)){
			sql.append(" and r. mobile=?");
			params.add(mobile);
		}	
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<RechargeRecord>();
		}
	}
}
